#include <gtest/gtest.h>
#include "../utils/test_utils.h"

using namespace TestUtils;

class DocumentationTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 文档测试不需要清理构建输出
    }
    
    void TearDown() override {
        // 文档测试不需要清理
    }
};

TEST_F(DocumentationTest, DocumentationStructure) {
    TestScope scope("DocumentationStructure");
    
    // 验证文档目录存在
    EXPECT_TRUE(directoryExists(Paths::DOCS_DIR)) 
        << "Documentation directory not found: " << Paths::DOCS_DIR;
    
    // 验证核心文档文件存在
    std::vector<std::string> requiredDocs = {
        "docs/Home.md",
        "docs/Get Started.md",
        "docs/Tutorials.md"
    };
    
    for (const auto& docFile : requiredDocs) {
        EXPECT_TRUE(fileExists(docFile)) 
            << "Required documentation file not found: " << docFile;
    }
}

TEST_F(DocumentationTest, DocumentationContent) {
    TestScope scope("DocumentationContent");
    
    // 验证Home.md内容
    std::string homeContent = readFile("docs/Home.md");
    EXPECT_FALSE(homeContent.empty()) << "Home.md is empty";
    EXPECT_GT(homeContent.length(), 100) << "Home.md content too short";
    
    // 验证Get Started.md内容
    std::string getStartedContent = readFile("docs/Get Started.md");
    EXPECT_FALSE(getStartedContent.empty()) << "Get Started.md is empty";
    EXPECT_GT(getStartedContent.length(), 100) << "Get Started.md content too short";
    
    // 验证Tutorials.md内容
    std::string tutorialsContent = readFile("docs/Tutorials.md");
    EXPECT_FALSE(tutorialsContent.empty()) << "Tutorials.md is empty";
    EXPECT_GT(tutorialsContent.length(), 100) << "Tutorials.md content too short";
}

TEST_F(DocumentationTest, HomeDocumentationStructure) {
    TestScope scope("HomeDocumentationStructure");
    
    std::string homeContent = readFile("docs/Home.md");
    ASSERT_FALSE(homeContent.empty()) << "Home.md not found or empty";
    
    // 验证Home.md包含基本信息
    EXPECT_TRUE(containsString(homeContent, "#")) 
        << "Home.md should contain markdown headers";
    
    // 验证包含项目相关信息
    EXPECT_TRUE(containsString(homeContent, "build") || 
                containsString(homeContent, "Build")) 
        << "Home.md should mention build system";
}

TEST_F(DocumentationTest, GetStartedDocumentation) {
    TestScope scope("GetStartedDocumentation");
    
    std::string getStartedContent = readFile("docs/Get Started.md");
    ASSERT_FALSE(getStartedContent.empty()) << "Get Started.md not found or empty";
    
    // 验证Get Started包含安装或使用说明
    EXPECT_TRUE(containsString(getStartedContent, "#")) 
        << "Get Started.md should contain markdown headers";
    
    // 验证包含实用信息
    bool hasUsefulContent = containsString(getStartedContent, "install") ||
                           containsString(getStartedContent, "setup") ||
                           containsString(getStartedContent, "build") ||
                           containsString(getStartedContent, "start");
    
    EXPECT_TRUE(hasUsefulContent) 
        << "Get Started.md should contain installation or setup instructions";
}

TEST_F(DocumentationTest, TutorialsDocumentation) {
    TestScope scope("TutorialsDocumentation");
    
    std::string tutorialsContent = readFile("docs/Tutorials.md");
    ASSERT_FALSE(tutorialsContent.empty()) << "Tutorials.md not found or empty";
    
    // 验证Tutorials包含教程内容
    EXPECT_TRUE(containsString(tutorialsContent, "#")) 
        << "Tutorials.md should contain markdown headers";
    
    // 验证包含教程相关内容
    bool hasTutorialContent = containsString(tutorialsContent, "tutorial") ||
                             containsString(tutorialsContent, "Tutorial") ||
                             containsString(tutorialsContent, "example") ||
                             containsString(tutorialsContent, "Example");
    
    EXPECT_TRUE(hasTutorialContent) 
        << "Tutorials.md should contain tutorial or example content";
}

TEST_F(DocumentationTest, MarkdownFormatValidation) {
    TestScope scope("MarkdownFormatValidation");
    
    std::vector<std::string> docFiles = {
        "docs/Home.md",
        "docs/Get Started.md",
        "docs/Tutorials.md"
    };
    
    for (const auto& docFile : docFiles) {
        std::string content = readFile(docFile);
        ASSERT_FALSE(content.empty()) << "Document file empty: " << docFile;
        
        // 基本的Markdown格式验证
        // 检查是否有标题
        EXPECT_TRUE(containsString(content, "#")) 
            << "Document should contain markdown headers: " << docFile;
        
        // 检查文件扩展名
        EXPECT_TRUE(docFile.size() >= 3 && docFile.substr(docFile.size() - 3) == ".md") 
            << "Document should have .md extension: " << docFile;
    }
}

TEST_F(DocumentationTest, DocumentationCompleteness) {
    TestScope scope("DocumentationCompleteness");
    
    // 验证README文件存在
    EXPECT_TRUE(fileExists("README.md")) 
        << "README.md file not found in project root";
    
    // 验证README内容
    std::string readmeContent = readFile("README.md");
    EXPECT_FALSE(readmeContent.empty()) << "README.md is empty";
    EXPECT_GT(readmeContent.length(), 200) << "README.md content too short";
    
    // 验证LICENSE文件存在
    EXPECT_TRUE(fileExists("LICENSE")) 
        << "LICENSE file not found in project root";
}

TEST_F(DocumentationTest, ExampleDocumentation) {
    TestScope scope("ExampleDocumentation");
    
    // 验证examples目录存在
    EXPECT_TRUE(directoryExists(Paths::EXAMPLES_DIR)) 
        << "Examples directory not found";
    
    // 查找examples中的README文件
    auto readmeFiles = findFiles(Paths::EXAMPLES_DIR, "README");
    
    // 至少应该有一些示例文档
    EXPECT_FALSE(readmeFiles.empty()) 
        << "No README files found in examples directory";
    
    // 验证找到的README文件有内容
    for (const auto& readmeFile : readmeFiles) {
        std::string content = readFile(readmeFile);
        EXPECT_FALSE(content.empty()) 
            << "Example README file is empty: " << readmeFile;
    }
}

TEST_F(DocumentationTest, DocumentationAccessibility) {
    TestScope scope("DocumentationAccessibility");
    
    // 验证文档文件权限（应该可读）
    std::vector<std::string> docFiles = {
        "README.md",
        "docs/Home.md",
        "docs/Get Started.md",
        "docs/Tutorials.md"
    };
    
    for (const auto& docFile : docFiles) {
        EXPECT_TRUE(fileExists(docFile)) << "Document file not accessible: " << docFile;
        
        // 尝试读取文件以验证权限
        std::string content = readFile(docFile);
        EXPECT_FALSE(content.empty()) 
            << "Cannot read document file or file is empty: " << docFile;
    }
}